<?php

namespace App\Http\Controllers\Goods;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Goods\Product;
use productsShow;
class ProductsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth', ['except' => ['index', 'show']]);
    }
    public function index(Request $request)
    {
        $date=productsShow::index($request);
        return view('goods.products.index', [
            'products' => $date['products'],
            'filters'  => [
                'search' => $date['search'],
                'order'  => $date['order'],
            ],
        ]);
    }
    public function show(Product $product, Request $request)
    {
        // 判断商品是否已经上架，如果没有上架则抛出异常。
        if (!$product->on_sale) {
            throw new \Exception('商品未上架');
        }
        $favored=productsShow::favored($request,$product);
        return view('goods.products.show', ['product' => $product, 'favored' => $favored]);
    }
    public function favorites(Request $request)
    {
        $products = $request->user()->favoriteProducts()->paginate(16);

        return view('goods.products.favorites', ['products' => $products]);
    }
    public function favor(Product $product, Request $request)
    {
        return productsShow::favor($request,$product);
    }
    public function disfavor(Product $product, Request $request)
    {
        return productsShow::disfavor($request,$product);
    }
}
